<template>
  <div class="topbar">
    <div class="wrap topbarWrap">
      <div class="left">欢迎来到口顶狼积分商城</div>
      <ul class="right">
        <li class="li-last">
          <span class="imgs">
            <img :src="userInfo.headImg" alt="" class="avatar" />
          </span>
          <span> 用户名:{{ userInfo.nickName }} </span>
        </li>
        <li>我的积分:{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>口顶狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="login-btn" v-else @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeixinLoginApi } from "@/request/api";
export default {
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  data() {
    return {};
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
      asyncChangeUserInfo: "userInfo/asyncChangeUserInfo",
    }),
  },
  created() {
    this.$router.onReady(async () => {
      // console.log("打印code", this.$route.query.code);
      if (this.$route.query.code) {
        //向后台发送请求
        let res = await WeixinLoginApi({
          code: this.$route.query.code,
        });
        // console.log("打印res", res);
        if (res.data.code == 0) {
          //弹框提示登录成功
          this.asyncShowToast({
            isShow: true,
            title: "登录成功",
            type: "success",
          });
          // console.log("直接扫码登录成功", res);
          //储存token
          localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
          //更新登录状态
          this.changeIsLogined(true);
          //刷新路由
          this.$router.push(this.$route.path);

          //登录成功的时候发起请求user用户数据并显示在topbar上
          this.initUserInfo();
        } else if (res.data.code == 400) {
          //400
          // console.log("400错误");
          //弹出提示框
          this.asyncShowToast({
            isShow: true,
            title: res.data.message,
            type: "danger",
          });
          //弹出模态框
          this.changeIsShowLoginModal(true);
          //刷新路由
          this.$router.push(this.$route.path);
          // console.log("完成400操作");
        } else if (res.data.code == 407) {
          // res.data.token
          //res.token undefined
          //提示没有绑定手机号
          console.log(res);
          //弹框提示
          this.asyncShowToast({
            isShow: true,
            title: res.data.message,
            type: "warning",
          });
          //设置uuid
          localStorage.setItem("uuid", res.data.uuid);
          //弹出模态框
          this.changeIsShowLoginModal(true);
          //刷新路由
          this.$router.push(this.$route.path);
        }
      } else {
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
        if (token) {
          this.asyncChangeUserInfo();
        } else {
          this.initUserInfo();
        }
      }
    });
  },
};
</script>

<style lang="less" scoped>
.topbar {
  background: #242b39;
  height: 40px;
  .topbarWrap {
    line-height: 40px;
    display: flex;
    justify-content: space-between;
    color: #fffefe;
    align-items: center;
    .left {
      font-size: 14px;
      font-weight: 300;
    }
    .right {
      li {
        float: left;
        margin-left: 20px;
        text-align: center;
        line-height: 40px;
        cursor: pointer;
        font-size: 14px;
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
      .avatar {
        width: 23px;
        height: 23px;
        border-radius: 50%;
        vertical-align: middle;
      }
      .li-last {
        display: flex;
        .imgs {
          font-size: 0;
          height: 40px;
        }
      }
      .login-btn {
        width: 124px;
        background: #0a328e;
      }
    }
  }
}
</style>